/*
	post:{
		broadcast:{number,type,msg:{number,msg}},
		gossip:{msg:{number,msg}},
		login{number,user}
	}
*/
var post=function(){
	var post,get,user;
	post = io.connect('http://127.0.0.1:8888');
	get=callback();
	post.on('regist', function(data) {
		get.regist(data);
	});
	post.on('loadIn',function(res){
		console.log('--loadIn');
		get.loadIn(res);
	});
	post.on('loginIn',function(res){
		console.log('--loginIn');
		get.loginIn(res);
	});
	post.on('gossip',function(data){
		console.log('--gossip');
		get.gossip(data);
	});
	post.on('searchFriend',function(data){
		console.log('--searchFriend');
		get.searchFriend(data);
	});
	post.on('broadcast',function(number,type,msg){
		console.log('--broadcast');
		get.broadcast(number,type,msg);
	});
	post.on('return', function(type,data) {
		get.returns(type,data);
	});
	post.on('err', function(type,data) {
		get.err(type,data);
	});
	function init(u){
		user=u;
		get.init(u);
		load();
	};
	function load(){
		post.emit('load');
	};
	function login(user) {
		post.emit('login',user);
	};
	function gossip(msg) {
		post.emit('gossip',msg);
		//判断是否已经存在于最近聊天列表
		if(!($('#dl-recently .f-'+msg.number)).length>0){
			addFriend(msg.number,1);
			//将好友加入最近聊天列表
			addRecentlyFriend(msg.number);
		}
	};
	function broadcast(type,msg){
		post.emit('broadcast',type,msg);
	};
	function regist(user){
		post.emit('regist',user);
	}
	function gossipOk(friendNumber){
		post.emit('gossipOk',user.getUser().number,friendNumber);
	}
	function searchFriend(friendNumber){
		post.emit('searchFriend',user.getUser().number,friendNumber);
	}
	function addFriend(friendNumber,listType){
		post.emit('addFriend',user.getUser().number,friendNumber,listType);
	}
	function delFriend(friendNumber,listType){
		post.emit('delFriend',user.getUser().number,friendNumber,listType);
	}
	return {
		'init':init,
		'broadcast':broadcast,
		'gossip':gossip,
		'login':login,
		'regist':regist,
		'gossipOk':gossipOk,
		'addFriend':addFriend,
		'delFriend':delFriend
	};
}
